//   Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#pragma once

#include <string>
#include <vector>
#include <memory>
#include <utility>
#include <ctime>
#include <mutex>

#include <opencv2/opencv.hpp>
#include <paddle_inference_api.h>

#include "base_util/utils.h"
#include "paddle/pdmodel_manager.h"


namespace ai {

class ImageBlob;

class PDDetPredictor {
public:
  PDDetPredictor(LogInfo *lg);
  ~PDDetPredictor(){}
  
  int Run(cv::Mat& img,
                std::vector<BaseInfo*>& det_infos, 
                PDModelManager* mng,
                int md_idx);

  int Run(std::vector<cv::Mat>& imgs,
                std::vector<std::vector<DetInfo>>& det_infos, 
                PDModelManager* mng,
                int md_idx);
private:

  int RunDet(cv::Mat& img,
                  std::vector<BaseInfo*>& det_infos, 
                  PDModelManager* mng,
                  int md_idx);

  int RunDet(std::vector<cv::Mat>& imgs,
                  std::vector<std::vector<DetInfo>>& det_infos, 
                  PDModelManager* mng,
                  int md_idx);


private:
  // Preprocess image and copy data to input buffer
  // void Preprocess(const cv::Mat& image_mat);
  // Postprocess result
  void output_box(
    std::vector<float>& output_data,
    cv::Mat& img,
    ImageBlob& img_info,
    std::vector<BaseInfo*>& det_infos,
    ModelConfig* cfg,
    const std::vector<float>& bbox={});

  void output_box(
    std::vector<float>& output_data,
    std::vector<cv::Mat>& imgs,
    std::vector<ImageBlob>& img_info,
    std::vector<std::vector<DetInfo>>& det_infos,
    ModelConfig* cfg);
    
  LogInfo *log_ifo;
  std::mutex rec_mut;

};

}  // namespace PaddleDetection
